package com.bulls.demo.hrmsdemo.dao;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.bulls.demo.hrmsdemo.db.Employee;
import com.bulls.demo.hrmsdemo.db.EmployeeExperience;
import com.bulls.demo.hrmsdemo.db.EmployeeQualification;
import com.bulls.demo.hrmsdemo.utils.Utilities;
import com.bulls.demo.hrmsdemo.vo.EmployeeVO;

public class EmployeeDAOImpl implements EmployeeDAO {

	@Override
	public Employee save(Employee employee, EntityManager entityManager) {
		EntityTransaction trans = entityManager.getTransaction();
		trans.begin();
		if(employee.getEmployeeid()!=null && employee.getEmployeeid()!=0){
			entityManager.merge(employee);
		}else{
			entityManager.persist(employee);
		}
		trans.commit();
		return employee;
	}

	@Override
	public ArrayList<EmployeeQualification> saveQualification(
			ArrayList<EmployeeQualification> employeeQualifications,
			EntityManager entityManager) {
		ArrayList<EmployeeQualification> persistedEmployeeQualification = new ArrayList<EmployeeQualification>();
		for(EmployeeQualification employeeQualification:employeeQualifications){
		if(Utilities.isNull(employeeQualification.getQualificationId())){
			entityManager.persist(employeeQualification);
		
		}else{
			entityManager.merge(employeeQualification);
		}
		persistedEmployeeQualification.add(employeeQualification);
		}
		return persistedEmployeeQualification;
	}

	@Override
	public ArrayList<EmployeeExperience> saveExperience(
			ArrayList<EmployeeExperience> employeeExperiences, EntityManager entityManager) {
		ArrayList<EmployeeExperience> persistedEmployeeExperience= new ArrayList<EmployeeExperience>();
		for(EmployeeExperience employeeExperience:employeeExperiences){
		if(Utilities.isNull(employeeExperience.getExperienceId())){
			entityManager.persist(employeeExperience);
		}else{
			entityManager.merge(employeeExperience);
		}
		persistedEmployeeExperience.add(employeeExperience);
		}
		return persistedEmployeeExperience;
	}

	@Override
	public EmployeeVO search(Long employeeID, EntityManager entityManager) {
		EmployeeVO employeeVO = new EmployeeVO();
		Employee employee = new Employee();
		ArrayList<EmployeeQualification> employeeQualifications = new ArrayList<EmployeeQualification>();
		ArrayList<EmployeeExperience> employeeExperiences = new ArrayList<EmployeeExperience>();
		employee  = entityManager.find(Employee.class, employeeID);
		Query query = entityManager.createNamedQuery("fetchEmployeeQualificationByEmployeeID");
		query.setParameter("employeeID",employeeID);
		employeeQualifications = (ArrayList<EmployeeQualification>) query.getResultList();
		query = entityManager.createNamedQuery("fetchEmployeeExperienceByEmployeeID");
		query.setParameter("employeeID",employeeID);
		employeeExperiences = (ArrayList<EmployeeExperience>) query.getResultList();
		employeeVO.setEmployee(employee);
		employeeVO.setEmployeeExperiences(employeeExperiences);
		employeeVO.setEmployeeQualifications(employeeQualifications);
		return employeeVO;
	}

	@Override
	public boolean delete(String employeeID, EntityManager entityManager) {
		boolean boo = true;
		try{
		Employee employee = new Employee();
		ArrayList<EmployeeQualification> employeeQualifications = new ArrayList<EmployeeQualification>();
		ArrayList<EmployeeExperience> employeeExperiences = new ArrayList<EmployeeExperience>();
		employee  = entityManager.find(Employee.class, employeeID);
		Query query = entityManager.createNamedQuery("fetchEmployeeQualificationByEmployeeID");
		query.setParameter("employeeID",employeeID);
		employeeQualifications = (ArrayList<EmployeeQualification>) query.getResultList();
		for(EmployeeQualification employeeQualification:employeeQualifications){
			entityManager.remove(employeeQualification);
		}
		query = entityManager.createNamedQuery("fetchEmployeeExperienceByEmployeeID");
		query.setParameter("employeeID",employeeID);
		employeeExperiences = (ArrayList<EmployeeExperience>) query.getResultList();
		for(EmployeeExperience employeeExperience:employeeExperiences){
			entityManager.remove(employeeExperience);
		}
		entityManager.remove(employee);
		}catch(Exception e){
			boo = false;
			e.printStackTrace();
		}
		return boo;
	}


}
